import useDictStore from '@/store/modules/dict'
import useShareDictStore from '@/store/modules/shareDict'
import useOrgDictStore from '@/store/modules/orgDict'
import { getDicts } from '@/api/system/dict/data'
import { getShareDicts } from '@/api/system/dict/data'
import { getOrgDicts } from '@/api/system/orgDict/data'

/**
 * 获取本系统字典数据
 */
export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType, index) => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getDicts(dictType).then(resp => {
          res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass, dictCode: p.dictCode }))
          useDictStore().setDict(dictType, res.value[dictType]);
        })
      }
    })
    return toRefs(res.value);
  })()
}

/**
 * 获取发布系统字典数据
 */
 export function useShareDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType, index) => {
      res.value[dictType] = [];
      const dicts = useShareDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getShareDicts(dictType).then(resp => {
          res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
          useShareDictStore().setDict(dictType, res.value[dictType]);
        })
      }
    })
    return toRefs(res.value);
  })()
}

/**
 * 获取发布系统字典数据
 */
 export function useShareDictWait(...args) {
  return new Promise((resolve, reject) => {
    const res = ref({});
    return (() => {
      args.forEach((dictType, index) => {
        res.value[dictType] = [];
        const dicts = useShareDictStore().getDict(dictType);
        if (dicts) {
          res.value[dictType] = dicts;
          resolve(res.value)
        } else {
          getShareDicts(dictType).then(resp => {
            res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
            useShareDictStore().setDict(dictType, res.value[dictType]);
            if (index == args.length-1) {
              resolve(res.value)
            }
          })
        }
      })
    })()
  })
}

/**
 * 获取本系统字典数据
 */
export function useOrgDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach((dictType, index) => {
      res.value[dictType] = [];
      const dicts = useOrgDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getOrgDicts(dictType).then(resp => {
          res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass, dictCode: p.dictCode,isDefault: p.isDefault }))
          useDictStore().setDict(dictType, res.value[dictType]);
        })
      }
    })
    return toRefs(res.value);
  })()
}